/*
 * Copyright (C) 2010-2025 Structr GmbH
 *
 * This file is part of Structr <http://structr.org>.
 *
 * Structr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Structr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Structr.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.structr.web.maintenance;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.structr.api.Predicate;
import org.structr.api.config.Settings;
import org.structr.common.error.FrameworkException;
import org.structr.core.Services;
import org.structr.core.graph.MaintenanceCommand;
import org.structr.core.graph.NodeServiceCommand;
import org.structr.core.graph.TransactionCommand;
import org.structr.docs.*;
import org.structr.schema.action.ActionContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MaintenanceModeCommand extends NodeServiceCommand implements MaintenanceCommand {

	private static final Logger logger = LoggerFactory.getLogger(MaintenanceModeCommand.class.getName());

	static {

	}

	@Override
	public void execute(Map<String, Object> parameters) throws FrameworkException {

		final String action = (String) parameters.get("action");
		boolean success = false;

		if ("enable".equals(action)) {

			success = Services.getInstance().setMaintenanceMode(true);

		} else if ("disable".equals(action)) {

			success = Services.getInstance().setMaintenanceMode(false);

		} else {

			logger.warn("Unsupported action '{}'", action);
		}

		if (success) {

			final Map<String, Object> msgData = new HashMap();
			msgData.put(MaintenanceCommand.COMMAND_TYPE_KEY, "MAINTENANCE");
			msgData.put("enabled", Settings.MaintenanceModeEnabled.getValue());
			msgData.put("baseUrl", ActionContext.getBaseUrl(securityContext.getRequest(), true));
			TransactionCommand.simpleBroadcastGenericMessage(msgData, Predicate.all());
		}
	}

	@Override
	public boolean requiresEnclosingTransaction() {
		return false;
	}

	@Override
	public boolean requiresFlushingOfCaches() {
		return false;
	}

	// ----- interface Documentable -----
	@Override
	public DocumentableType getDocumentableType() {
		return DocumentableType.MaintenanceCommand;
	}

	@Override
	public String getName() {
		return "maintenanceMode";
	}

	@Override
	public String getShortDescription() {
		return "Enables or disables the maintenance mode.";
	}

	@Override
	public String getLongDescription() {
		return """
		When the maintenance mode is started, the following services are shut down:

		- FtpService
		- HttpService
		- SSHService
		- AgentService
		- CronService
		- DirectoryWatchService
		- LDAPService
		- MailService

		After a short delay, the following services are restarted on different ports:
		- FtpService
		- HttpService
		- SSHService
		""";
	}

	@Override
	public List<Parameter> getParameters() {
		return List.of(
			Parameter.mandatory("action", "`enable` or `disable`")
		);
	}

	@Override
	public List<Example> getExamples() {
		return List.of();
	}

	@Override
	public List<String> getNotes() {
		return List.of(
			"Active processes will keep running until they are finished. If for example a cron job is running, it will not be halted. Only the services are stopped so no NEW processes are started."
		);
	}

	@Override
	public List<Signature> getSignatures() {
		return List.of();
	}

	@Override
	public List<Language> getLanguages() {
		return List.of();
	}

	@Override
	public List<Usage> getUsages() {
		return List.of();
	}
}
